//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.rpt;

import com.aidong.common.JobConsts;
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.ReportFileListDao;
import com.aidong.model.wills.ReportFileList;
import com.aidong.web.controller.BaseController;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"rptFileDownload"})
public class RptFileDownloadController extends BaseController {
    @Resource
    ReportFileListDao reportFileListDao;

    public RptFileDownloadController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateFrom = sdf.format(new Date());
        String dateTo = sdf.format(new Date());
        ModelAndView mv = new ModelAndView("rpt/filedownload/list");
        mv.addObject("reportFileTypeName", this.getTypeList("reportFileTypeName"));
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("dateFrom", dateFrom);
        mv.addObject("dateTo", dateTo);
        return mv;
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "reportFileType",required = false) String reportFileType, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "ShopCode", shopCode);
        if(!this.chkNull(dateFrom)) {
            cd.ge("ReportCreateDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("ReportCreateDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        if(!this.chkNull(reportFileType)) {
            cd.eq("ReportFileType", reportFileType);
        }

        this.reportFileListDao.queryPageList(pg, cd, (new OrderSet()).asc("AutoId"));
        ModelAndView mv = new ModelAndView("rpt/filedownload/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("reportFileTypeName", this.getTypeList("reportFileTypeName"));
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"_downloadzip"})
    public ModelAndView _downloadzip(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "reportFileType",required = false) String reportFileType, HttpServletRequest request, HttpServletResponse response) {
        Pagination pg = new Pagination(1, 500);
        pg.setTotalCount(1);
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "ShopCode", shopCode);
        if(!this.chkNull(dateFrom)) {
            cd.ge("ReportCreateDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("ReportCreateDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        if(!this.chkNull(reportFileType)) {
            cd.eq("ReportFileType", reportFileType);
        }

        this.reportFileListDao.queryPageList(pg, cd, (new OrderSet()).asc("AutoId"));
        if(pg.getResultList() != null && pg.getResultList().size() != 0) {
            if(pg.getResultList().size() > 200) {
                return this.showAlert("一次下载的文件不能超过200个");
            } else {
                try {
                    response.setContentType("application/zip");
                    response.setHeader("Content-disposition", "attachment; filename=download.zip");
                    CheckedOutputStream e = new CheckedOutputStream(response.getOutputStream(), new CRC32());
                    ZipOutputStream zos = new ZipOutputStream(e);
                    byte[] buf = new byte[1024];
                    Iterator i$ = pg.getResultList().iterator();

                    while(true) {
                        ReportFileList file;
                        File f;
                        do {
                            if(!i$.hasNext()) {
                                zos.close();
                                zos.flush();
                                this.addUserLog("打包下载报表文件", pg.getResultList().size() + "个文件");
                                return null;
                            }

                            file = (ReportFileList)i$.next();
                            f = new File(this.getSetting(JobConsts.EXCEL_OUT_PATH_KEY) + file.getReportFilePath() + File.separator + file.getReportFileName());
                        } while(!f.exists());

                        ZipEntry entry = new ZipEntry(file.getReportFileChName());
                        zos.putNextEntry(entry);
                        FileInputStream is = new FileInputStream(f);

                        int n;
                        while((n = is.read(buf)) != -1) {
                            zos.write(buf, 0, n);
                        }

                        is.close();
                    }
                } catch (Exception var18) {
                    this.log.error("download", var18);
                    return null;
                }
            }
        } else {
            return this.showAlert("没有可下载的文件");
        }
    }

    @RequestMapping({"_download"})
    private String _download(@RequestParam(value = "autoId",required = false) String autoId, HttpServletRequest request, HttpServletResponse response) {
        ReportFileList reportFileList = (ReportFileList)this.reportFileListDao.getById(Integer.valueOf(Integer.parseInt(autoId)));
        String fileChName = reportFileList.getReportFileChName();
        String filePath = reportFileList.getReportFilePath();
        String fileName = reportFileList.getReportFileName();
        ServletOutputStream os = null;
        FileInputStream is = null;

        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileChName.getBytes(), "iso-8859-1"));
            String e2 = this.getSetting(JobConsts.EXCEL_OUT_PATH_KEY) + filePath;
            is = new FileInputStream(new File(e2 + File.separator + fileName));
            os = response.getOutputStream();
            byte[] b = new byte[2048];

            int length;
            while((length = is.read(b)) > 0) {
                os.write(b, 0, length);
            }

            os.close();
            is.close();
        } catch (FileNotFoundException var28) {
            var28.printStackTrace();
        } catch (IOException var29) {
            var29.printStackTrace();
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException var27) {
                    ;
                }
            }

            if(is != null) {
                try {
                    is.close();
                } catch (IOException var26) {
                    ;
                }
            }

        }

        return null;
    }
}
